# CMake minimum version requirement
cmake_minimum_required(VERSION 3.16)

# Project declaration
project(image2pcd)

# Set C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set build type to Release if not specified
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -fopenmp")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

# Find required packages using pkg-config to avoid VTK dependency issues
find_package(PkgConfig REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(yaml-cpp REQUIRED)

# Use pkg-config for PCL to avoid VTK dependencies
pkg_check_modules(PCL REQUIRED pcl_common-1.12 pcl_io-1.12 pcl_filters-1.12 pcl_registration-1.12 pcl_segmentation-1.12 pcl_sample_consensus-1.12 pcl_search-1.12 pcl_kdtree-1.12 pcl_features-1.12 pcl_visualization-1.12)

# Print found package information
message(STATUS "OpenCV version: ${OpenCV_VERSION}")
message(STATUS "PCL found via pkg-config")
message(STATUS "Eigen3 version: ${Eigen3_VERSION}")

# Include directories
include_directories(
  ${PCL_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIR}
  /usr/include/vtk
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Add PCL definitions
add_definitions(${PCL_CFLAGS_OTHER})

# Source files
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_LIST)

# Set output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

# Create executable
add_executable(${PROJECT_NAME} ${SRC_LIST})

# Link libraries
target_link_libraries(${PROJECT_NAME}
  ${OpenCV_LIBS}
  ${PCL_LIBRARIES}
  yaml-cpp
  pthread
  # VTK libraries needed for PCL visualization
  vtksys
  vtkCommonCore
  vtkCommonDataModel
  vtkCommonExecutionModel
  vtkCommonMath
  vtkCommonTransforms
  vtkRenderingCore
  vtkRenderingOpenGL2
  vtkRenderingLOD
  vtkRenderingFreeType
  vtkInteractionStyle
  vtkInteractionWidgets
  vtkFiltersCore
  vtkFiltersSources
  vtkFiltersExtraction
  vtkFiltersGeometry
  vtkIOCore
  vtkIOGeometry
  vtkIOLegacy
)
